package org.jeecg.modules.lab.utensil.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.utensil.entity.LabUtensilNote;
import org.jeecg.modules.lab.utensil.service.ILabUtensilNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
* @Description: 器具确认单记录
* @Author: jeecg-boot
* @Date:   2023-09-22
* @Version: V1.0
*/
@Api(tags="器具确认单记录")
@RestController
@RequestMapping("/utensil/labUtensilNote")
@Slf4j
public class LabUtensilNoteController extends JeecgController<LabUtensilNote, ILabUtensilNoteService> {
   @Autowired
   private ILabUtensilNoteService labUtensilNoteService;
   @Autowired
   private ILabCronJobService labCronJobService;

   /**
    * 分页列表查询
    *
    * @param labUtensilNote
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   //@AutoLog(value = "器具确认单记录-分页列表查询")
   @ApiOperation(value="器具确认单记录-分页列表查询", notes="器具确认单记录-分页列表查询")
   @GetMapping(value = "/list")
   public Result<IPage<LabUtensilNote>> queryPageList(LabUtensilNote labUtensilNote,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<LabUtensilNote> queryWrapper = QueryGenerator.initQueryWrapper(labUtensilNote, req.getParameterMap());
       Page<LabUtensilNote> page = new Page<LabUtensilNote>(pageNo, pageSize);
       IPage<LabUtensilNote> pageList = labUtensilNoteService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param labUtensilNote
    * @return
    */
   @AutoLog(value = "器具确认单记录-添加")
   @ApiOperation(value="器具确认单记录-添加", notes="器具确认单记录-添加")
   @RequiresPermissions("utensil_note:lab_utensil_note:add")
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody LabUtensilNote labUtensilNote) {
       labUtensilNoteService.save(labUtensilNote);

       if (MyEnum.sync) {
           long time = new Date().getTime();
           LabCronJob job = new LabCronJob("lab_utensil_note", "0", time, 1);
           labCronJobService.save(job);
       }
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param labUtensilNote
    * @return
    */
   @AutoLog(value = "器具确认单记录-编辑")
   @ApiOperation(value="器具确认单记录-编辑", notes="器具确认单记录-编辑")
   @RequiresPermissions("utensil_note:lab_utensil_note:edit")
   @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
   public Result<String> edit(@RequestBody LabUtensilNote labUtensilNote) {
       labUtensilNote.setSynFlag("1");
       labUtensilNoteService.updateById(labUtensilNote);

       if (MyEnum.sync) {
           long time = new Date().getTime();
           LabCronJob job = new LabCronJob("lab_utensil_note", "0", time, 1);
           labCronJobService.save(job);
       }
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "器具确认单记录-通过id删除")
   @ApiOperation(value="器具确认单记录-通过id删除", notes="器具确认单记录-通过id删除")
   @RequiresPermissions("utensil_note:lab_utensil_note:delete")
   @DeleteMapping(value = "/delete")
   public Result<String> delete(@RequestParam(name="id",required=true) String id) {
       labUtensilNoteService.removeById(id);

       if (MyEnum.sync) {
           long time = new Date().getTime();
           String condition = "id='" + id + "'";
           LabCronJob job = new LabCronJob("lab_utensil_note", "2", condition, time, 1);
           labCronJobService.save(job);
       }
       return Result.OK("删除成功!");
   }

   @GetMapping(value = "/queryNote")
   public Result<List<LabUtensilNote>> queryNote(@RequestParam(name = "id",defaultValue = "")String uteId){
       List<LabUtensilNote> list = labUtensilNoteService.list(new LambdaQueryWrapper<LabUtensilNote>().eq(LabUtensilNote::getUteId, uteId));
       return  Result.ok(list);
   }
   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "器具确认单记录-批量删除")
   @ApiOperation(value="器具确认单记录-批量删除", notes="器具确认单记录-批量删除")
   @RequiresPermissions("utensil_note:lab_utensil_note:deleteBatch")
   @DeleteMapping(value = "/deleteBatch")
   public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.labUtensilNoteService.removeByIds(Arrays.asList(ids.split(",")));

       if (MyEnum.sync) {
           long time = new Date().getTime();
           String[] idArray = ids.split(",");
           labCronJobService.saveCronBatch("lab_utensil_note","2",time,idArray);
       }
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   //@AutoLog(value = "器具确认单记录-通过id查询")
   @ApiOperation(value="器具确认单记录-通过id查询", notes="器具确认单记录-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<LabUtensilNote> queryById(@RequestParam(name="id",required=true) String id) {
       LabUtensilNote labUtensilNote = labUtensilNoteService.getById(id);
       if(labUtensilNote==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(labUtensilNote);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param labUtensilNote
   */
   @RequiresPermissions("utensil_note:lab_utensil_note:exportXls")
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, LabUtensilNote labUtensilNote) {
       return super.exportXls(request, labUtensilNote, LabUtensilNote.class, "器具确认单记录");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequiresPermissions("utensil_note:lab_utensil_note:importExcel")
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, LabUtensilNote.class);
   }

}
